const App = getApp()
Page({
  data: {
    navbar: [{
        name: '进行中',
        type: 'doing'
      },
      {
        name: '已完成',
        type: 'success'
      },
      {
        name: '已取消',
        type: 'cancel'
      }
    ],
    tabindex: 0,
    list: [],
    nomore: false,
    page: 1,
    type: 'doing',
    pageShow:false,
    setInter: '', //定时器
    src:'',
    hasorder:''
  },
  onLoad: function (options) {
  },
  onReady: function () {
    this.audioCtx = wx.createAudioContext('myAudio')
  },
  onShow: function () {
    let that = this
    that.list()
    that.data.setInter = setInterval(
      function () {
        that.getLocation()
      }, 30000);
  },
  // 实时更新司机经纬度
  getLocation() {
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        App.api.$http({
          url: 'Index/updateLocation',
          method: 'POST',
          data: {
            longitude: res.longitude,
            latitude: res.latitude
          }
        }, {}).then(res => {
          that.setData({
            hasorder:res.data.hasorder,
            src:res.data.mp3
          })
          if (res.data.has) {
            that.audioCtx.play()
          }
        })
      }
    })
  },
  list() {
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    App.api.$http({
      url: 'Oilorder/staffLists',
      method: 'Post',
      data: {
        page: that.data.page,
        datatype: that.data.type
      }
    }, {}).then(res => {
      that.setData({
        pageShow:true
      })
      wx.hideNavigationBarLoading()
      wx.hideLoading()
      wx.stopPullDownRefresh();
      if (that.data.page == 1) {
        that.setData({
          list: []
        })
      }
      if (that.data.page < res.data.list.last_page) {
        let hotPage = that.data.page + 1;
        that.setData({
          page: hotPage,
          nomore: true
        })
      } else {
        that.setData({
          nomore: false
        })
      }
      let lists = that.data.list.concat(res.data.list.data)
      that.setData({
        list: lists
      })
    })
  },

  bindHeaderTap(e) {
    let that = this
    let index = e.target.dataset.index
    that.setData({
      tabindex: index,
      type: that.data.navbar[index].type,
      page:1,
    })
    that.list()
  },
  phone(e) {
    App.api.$makePhone(e)
  },
  onHide: function () {
    let that = this
    clearInterval(that.data.setInter)
  },
  mapinfo(e) {
    let that = this
    console.log(e)
    const latitude = Number(e.currentTarget.dataset.info.latitude)
    const longitude = Number(e.currentTarget.dataset.info.longitude)
    const address=e.currentTarget.dataset.info.address
    wx.openLocation({
      latitude,
      longitude,
      address,
      scale: 15
    })
    // wx.navigateTo({
    //   url: '/pages/map/map?info=' + JSON.stringify(e.currentTarget.dataset.info),
    // })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    clearInterval(that.data.setInter)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: '刷新中...',
    })
    this.list()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.nomore) {
      this.list();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})